home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / FlyThrough 1.1.2 / src / Source / QD3D General Tools / C3DMFReader.cp next >
Encoding:
Text File  |  1997-03-01  |  2.5 KB  |  130 lines  |  [TEXT/CWIE]

  1. //
  2. //    C3DMFReader.cp
  3. //
  4. //    A class for building a Display TQ3GroupObject
  5. //
  6. //    by James Jennings
  7. //    February 28, 1997
  8. //
  9.  
  10. #include "C3DMFReader.h"
  11. #include "CShaderMakers.h"
  12. #include "StQ3Disposer.h"
  13.  
  14. #pragma mark === StObjectReader ===
  15. // StObjectReader wraps the reading of files from a TQ3ObjectFile
  16. class StObjectReader : public CObjectMaker<TQ3Object> {
  17. public:
  18.     virtual void    ReadObjectFrom(TQ3FileObject inFile);
  19.     
  20.     virtual Boolean IsNull() { return mObject==nil; }    
  21.     virtual Boolean IsViewHints();
  22.     virtual Boolean IsDrawable();
  23.     
  24. protected:
  25.     virtual void    Make() { Assert_(false /* Make() should never be called */); }
  26. };
  27.  
  28. void    StObjectReader::ReadObjectFrom(TQ3FileObject inFile)
  29. {
  30.     Assert_(inFile != nil);
  31.     Q3Forget(mObject);
  32.     mObject = ::Q3File_ReadObject(inFile);
  33. }
  34.  
  35. Boolean StObjectReader::IsViewHints()
  36. {
  37.     Assert_(mObject!=nil);
  38.     return ::Q3Object_IsType(mObject, kQ3SharedTypeViewHints) == kQ3True;
  39. }
  40.  
  41. Boolean StObjectReader::IsDrawable()
  42. {
  43.     Assert_(mObject!=nil);
  44.     return ::Q3Object_IsDrawable(mObject) == kQ3True;
  45. }
  46.  
  47.  
  48. #pragma mark === C3DMFReader ===
  49.  
  50. C3DMFReader::C3DMFReader(FSSpec *inFSSpec)
  51.     : mViewHints(0), mFile(0)
  52. {
  53.     Assert_(inFSSpec != nil);
  54.     TQ3Status theStatus;
  55.     TQ3StorageObject theStorage;
  56.     
  57.     try{
  58.         
  59.         theStorage = ::Q3FSSpecStorage_New(inFSSpec);
  60.         ThrowIfNil_(theStorage);
  61.         
  62.         mFile = ::Q3File_New();
  63.         ThrowIfNil_(mFile);
  64.         
  65.         theStatus = ::Q3File_SetStorage(mFile, theStorage);
  66.         ThrowIfQ3Fail_(theStatus);
  67.         Q3Forget(theStorage);
  68.         
  69.     }
  70.     catch(...) {
  71.         Q3Forget(mFile);
  72.         Q3Forget(theStorage);
  73.         throw;
  74.     }
  75. }
  76.  
  77. C3DMFReader::~C3DMFReader()
  78. {
  79.     Q3Forget(mViewHints);
  80.     Q3Forget(mFile);
  81. }
  82.  
  83. TQ3GroupPosition C3DMFReader::Add( TQ3GeometryObject inObject )
  84. {
  85.     Assert_( mObject != nil );
  86.     
  87.     TQ3GroupPosition pos;
  88.     
  89.     pos = ::Q3Group_AddObject( mObject, inObject );    
  90.     ThrowIf_(pos==0);
  91.     
  92.     return pos;
  93. }
  94.  
  95. void    C3DMFReader::Make()
  96. {
  97.     TQ3Status theStatus;
  98.     
  99.     mObject = ::Q3DisplayGroup_New();
  100.     ThrowIfNil_( mObject );
  101.     
  102.     // Add a shader.
  103.     CPhongShaderMaker shader;
  104.     Add(shader.Get());
  105.     
  106.     theStatus = ::Q3File_OpenRead(mFile,nil);
  107.     ThrowIfQ3Fail_(theStatus);
  108.     
  109.     StObjectReader theObject;
  110.     
  111.     while (::Q3File_IsEndOfFile(mFile) == kQ3False) {
  112.         
  113.         theObject.ReadObjectFrom( mFile );
  114.         if (theObject.IsNull()) continue;
  115.         
  116.         if ( theObject.IsViewHints() && mViewHints == nil )
  117.             mViewHints = theObject.GetRef();
  118.         
  119.         if (theObject.IsDrawable())
  120.             Add(theObject.Get());
  121.         
  122.     }
  123.     
  124.     // The file will be closed automatically when mFile is disposed
  125.     // but it's nice to be explicit about it.
  126.     theStatus = ::Q3File_Close(mFile);
  127.     ThrowIfQ3Fail_(theStatus);
  128. }
  129.  
  130.